package com.bric.common;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

import org.apache.log4j.Logger;

public class String_File {
	private static final Logger log=Logger.getLogger(String_File.class);
	/**
	 * 
	 * @param map  key为文件名称，value为文件所需要存的字符串
	 * @param rootpath  文件所在目录
	 * @return
	 */
	public static boolean str2File(Map<String,String> map,String rootpath){
		boolean flag=true;
		if(null!=map&&map.size()>0){
			File file=new File(rootpath);
			if(!file.exists()){
				file.mkdirs();
				}
			for(String key:map.keySet()){
				File f=new File(rootpath+key);
				try (BufferedWriter bw=new BufferedWriter(new FileWriter(f));){
					bw.write(map.get(key));
				} catch (FileNotFoundException e) {
					log.info("文件找不到"+e.getMessage());
					flag=false;
				} catch (IOException e1) {
					log.info("IO异常"+e1.getMessage());
					flag=false;
				}
			}
		}
		return flag;
	} 
	/**
	 * 
	 * @param fileName 文件名称
	 * @param strText 文件内容
	 * @param rootpath  根路径
	 * @return
	 */
	public static boolean str2File(String fileName,String strText,String rootpath){
			boolean flag=true;
				File file=new File(rootpath);
				if(!file.exists()){
					file.mkdirs();
					}
				File f=new File(rootpath+fileName);
				try (BufferedWriter bw=new BufferedWriter(new FileWriter(f));){
					bw.write(strText);
				} catch (FileNotFoundException e) {
					log.info("文件找不到"+e.getMessage());
					flag=false;
				} catch (IOException e1) {
					log.info("IO异常"+e1.getMessage());
					flag=false;
				}
		return flag;
	} 
	/*public static void main(String[] args) {
		Map<String,String> map=new HashMap<String,String>();
		String rootpath=PropertiesUtil.getValue("prewarnFilePath");
		map.put("pingguo.txt", "asdasssssssssss中文ssssssssssssss");
		str2File(map, rootpath);
	}*/
	public static String file2String(File file){
		try (BufferedReader br=new BufferedReader(new FileReader(file));){
			String temp=null;
			StringBuffer sb=new StringBuffer();
			while((temp=br.readLine())!=null){
				sb.append(temp);
			}
			return sb.toString();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			log.info("");
		} catch (IOException e) {
			log.info("");
		}
		return null;
	}
	/*public static void main(String[] args) {
		System.out.println(file2String(new File("D:/data.txt")));
	}*/
	/**
	 * 
	 * @param sourceFile  源文件
	 * @param targetFilepath  目标文件路径
	 * @return
	 */
	public static boolean file2file(File sourceFile,String targetFilepath){
		File file=new File(targetFilepath);
		boolean flag=false;
		if(!file.exists()){
			file.mkdirs();
		}
		BufferedInputStream bis=null;
		BufferedOutputStream bos=null;
		File target=new File(targetFilepath+sourceFile.getName());
		try {
			if(target.exists()){
				log.info(target.getAbsolutePath()+"文件已经存在，需要先删除再重写");
				target.delete();
			}
			bis=new BufferedInputStream(new FileInputStream(sourceFile));
			bos=new BufferedOutputStream(new FileOutputStream(target));
			byte[] bytes=new byte[1024];
			int len=0;
			while((len=bis.read(bytes))!=-1){
				bos.write(bytes, 0, len);
			}
			/*String line=null;
			while((line=br.readLine())!=null){
				bw.write(line);
			}*/
			flag=true;
		} catch (FileNotFoundException e) {
			log.info("文件未找到异常"+e.getMessage());
		} catch (IOException e) {
			log.info("IO异常"+e.getMessage());
		}finally{
			try {
				if(null!=bis){
					bis.close();
				}
				if(null!=bos){
					bos.close();
				}
			} catch (Exception e2) {
				log.info("关闭流出现异常");
			}
		}
		return flag;
	}
}
